package com.gzsz.service;

import com.gzsz.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import static java.lang.System.out;

public class DoctorServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException{
        DBUtil dbUtil=new DBUtil();
        out.println("entry -----");
        String method= request.getParameter("method");
        if (method.equals("delete")){
            String doctor_id= request.getParameter("doctor_id");
            String delete_sql="delete from doctor where doctor_id="+doctor_id+";";
            if (dbUtil.executeUpdateBySQL(delete_sql)>0){
                resp.sendRedirect("/webapps_Web_exploded/doctor?doctor_name=&title=&method=search");
            }else {
                out.println("删除失败");
            }
        } else if (method.equals("search")){
            String doctor_name = request.getParameter("doctor_name"); // 直接赋值，无需初始化
            String title = request.getParameter("title");
            String query_sql = "select * from doctor";
            // 根据参数构建SQL查询
            if ((doctor_name == null || doctor_name.isEmpty()) && (title == null || title.isEmpty())) {
                // 无搜索条件，查询所有医生
                query_sql = "select * from doctor";
            }
            else if ((doctor_name == null || doctor_name.isEmpty()) && title != null && !title.isEmpty()) {
                // 仅按title搜索
                query_sql = "select * from doctor where title like '%" + title + "%'";
            }
            else if (doctor_name != null && !doctor_name.isEmpty() && (title == null || title.isEmpty())) {
                // 仅按doctor_name搜索
                query_sql = "select * from doctor where doctor_name like '%" + doctor_name + "%'";
            }
            else if (doctor_name != null && !doctor_name.isEmpty() && title != null && !title.isEmpty()) {
                // 按doctor_name和title搜索
                query_sql = "select * from doctor where title like '%" + title + "%' and doctor_name like '%" + doctor_name + "%'";
            }
            // 执行查询
            ResultSet resultSet = dbUtil.queryBySQL(query_sql);
            request.setAttribute("doctor_list", resultSet);
            request.getRequestDispatcher("/html1/doctor.jsp").forward(request, resp);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException{
        String method= request.getParameter("method");
        DBUtil dbUtil=new DBUtil();
        if (method.equals("add")){
            //1.获取表单的值
            String doctor_name = request.getParameter("doctor_name");
            String gender = request.getParameter("gender");
            String age = request.getParameter("age");
            String title = request.getParameter("title");
            String contact_number = request.getParameter("contact_number");
            String email = request.getParameter("email");
            //2.拼接SQL语句执行数据库
            String add_sql="INSERT INTO doctor (doctor_name, gender, age, title, contact_number, email) " +
                    "VALUES ('"+doctor_name+"', '"+gender+"', '"+age+"', '"+title+"', '"+contact_number+"', '"+email+"');";
            if (dbUtil.executeUpdateBySQL(add_sql)>0){
                resp.sendRedirect("/webapps_Web_exploded/doctor?doctor_name=&title=&method=search");
            }else {
                out.println("添加失败");
            }
        } else if (method.equals("edit")) {

            //1.获取表单的值
            String doctor_id = request.getParameter("doctor_id");
            String doctor_name = request.getParameter("doctor_name");
            String gender = request.getParameter("gender");
            String age = request.getParameter("age");
            String title = request.getParameter("title");
            String contact_number = request.getParameter("contact_number");
            String email = request.getParameter("email");
            //2.拼接SQL语句执行数据库
            String edit_sql="UPDATE doctor SET doctor_id='"+doctor_id+"',doctor_name='"+doctor_name+"',gender='"+gender+"',age='"+age+"',title='"+title+"',contact_number='"+contact_number+"',email='"+email+"' where doctor_id='"+doctor_id+"'";

            if (dbUtil.executeUpdateBySQL(edit_sql)>0){
//                System.out.println("编辑成功");
                resp.sendRedirect("/webapps_Web_exploded/doctor?doctor_name=&title=&method=search");
            }else {
                out.println("编辑失败");
            }
        }
    }
}




